package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayExpert;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayQuestion;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author：贾琦磊
 * @Date：2018/7/19
 * @Description：问答功能
 */
@Service
public class SundayQuestionService extends BaseMongoService<SundayQuestion>{
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<SundayQuestion> getEntityClass() {
        return SundayQuestion.class;
    }

    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayExpertService expertService;

    /**
     * 保存问答
     * @param answerId
     * @param memberId
     * @param parentId
     * @param content
     * web端只能回复用户问题，所以parentId就是用户问题的id,后台管理员回答的问题直接用于展示
     */
    public void save( Long answerId,Long memberId, Long parentId,String content){
        SundayQuestion question=new SundayQuestion(memberId,parentId,content,SundayCommonConstants.comment_type_pass);
        if(LongUtil.isNotZreo(answerId)){
            super.updateExceptEmpty(answerId,question);
        }else {
            super.save(question);
        }
    }

    /**
     * 分页查询所有的问题
     * @param request
     * @return
     */
    public PageResult<SundayQuestion> select(HttpServletRequest request, Long parentId){
        PageResult<SundayQuestion> result=new PageResult<>();
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"status");
        param.put("isDeleted", SundayCommonConstants.NO);
        if(LongUtil.isNotZreo(parentId)){//根据父id获取所有回答
            param.put("parentId",parentId);
        }else {
            //获取所有提问
            param.put("parentId",0);
        }
        PageResult<SundayQuestion> pageResult = super.containpageQuery(request, null, param);
        List<SundayQuestion> data = pageResult.getData();
        List<Long> memberIds=new ArrayList<>();
        List<Long> expertIds=new ArrayList<>();
        for (SundayQuestion question : data) {
            //用户提问的问题
            if(question.getParentId().longValue()==0){
                memberIds.add(question.getMemberId());
            }else{
                expertIds.add(question.getMemberId());
            }

        }
        //查询出所有相关用户信息
        List<SundayMember> members=new ArrayList<>();
        if(ListUtil.isNotNull(memberIds)){
            param.clear();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("id in",memberIds);
            members = memberService.list(param);
        }
        //查询出所有相关用户信息
        List<SundayExpert> experts=new ArrayList<>();
        if(ListUtil.isNotNull(expertIds)){
            param.clear();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("id in",expertIds);
            experts = expertService.list(param);
        }
        for (SundayQuestion question: data) {
            if(question.getParentId().longValue()==0){
                for (SundayMember member:members) {
                    if(question.getMemberId().equals(member.getId())){
                        question.setMemberName(member.getNickName());
                        question.setIdentity(member.getUserType());
                        question.setImage(member.getImage());
                        break;
                    }
                }
            }else {
                for (SundayExpert expert:experts) {
                    if(question.getMemberId().equals(expert.getId())){
                        question.setMemberName(expert.getName());
                        question.setIdentity(SundayCommonConstants.user_type_expert);
                        question.setImage(expert.getImage());
                        break;
                    }
                }
            }
        }
        result.setData(data);
        result.setTotal(pageResult.getTotal());
        return result;
    }

    /**
     * 审核
     * @param id
     * @param status
     */
    public void setStatus(Long id,Integer status){
        super.update(id,"status",status);
    }

    public SundayQuestion findOne(Long id){
        SundayQuestion question = super.getById(id);
        if(question==null){
            return null;
        }
        if(LongUtil.isNotZreo(question.getMemberId())){
            if(question.getParentId().longValue()==0){
                SundayMember member = memberService.getById(question.getMemberId());
                question.setMemberName(member.getNickName());
                question.setIdentity(member.getUserType());
                question.setImage(member.getImage());
            }else {
                SundayExpert expert = expertService.getById(question.getMemberId());
                question.setMemberName(expert.getName());
                question.setIdentity(SundayCommonConstants.user_type_expert);
                question.setImage(expert.getImage());
            }
        }
        return question;
    }

}
